<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreMaterialRequest;
use App\Models\Material;
use App\Models\MaterialContract;
use App\Services\MaterialService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class MaterialController extends Controller
{
    protected $MaterialService;
    public function __construct(MaterialService $MaterialService)
    {
        $this->MaterialService = $MaterialService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $type = $Request->type ?? 1;
        $apply = $Request->apply;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->MaterialService->get($search,$type);
        $sql->where('project_id',$Request->project_id);
        if($apply){
            $sql->with('payment_apply');
        }
        if($Request->contract){
            $sql->with('contract');
        }
        $sql->where('user_id',Auth::user()->id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]->is_process = false;
            if($v->application){
                $data[$k]->is_process = $v->application->is_process;
            }
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function cost(Request $Request) {
        $data = $this->MaterialService->contractCost($Request->project_id,$Request->contract_type);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function contractPayCost(Request $Request) {
        $data = $this->MaterialService->contractPayCost($Request->project_id,$Request->source_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Material::with('project')->where('id',$Request->id)->first();
        $data->date = Date("Y-m-d",time());
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function contractShow(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = MaterialContract::with('application')->with('details')->with('user')->where('id',$Request->id)->first();
        $data->date = Date("Y-m-d",time());
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function contract(Request $Request) {

        $data = MaterialContract::where('user_id',Auth::user()->id)->with('application')->where('project_id',$Request->project_id)->get();
        foreach ($data as $k => $v) {
            $data[$k]->is_process = false;
            if($v->application){
                $data[$k]->is_process = $v->application->is_process;
            }
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }



    /**
     * 存储一个新数据。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreMaterialRequest $Request)
    {
        $POSTONLY = Material::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['company_id'] = Auth::user()->company_id;
        $input['user_id'] = Auth::user()->id;
        $data = $this->MaterialService->insert($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(StoreMaterialRequest $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = Material::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['user_id'] = Auth::user()->id;
        $input['id'] = $Request->id;
        $data = $this->MaterialService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function storeContract(Request $Request)
    {
        $POSTONLY = MaterialContract::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['user_id'] = Auth::user()->id;
        $data = $this->MaterialService->insertContract($input,$Request->details);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function updateContract(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = MaterialContract::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['user_id'] = Auth::user()->id;
        $input['id'] = $Request->id;
        $data = $this->MaterialService->updateContract($input,$Request->details);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Material::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function deleteContract(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = MaterialContract::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }
}
